/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月10日         luwei
 */

package com.coocaa.launcher.guide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.utils.ThreadPool;

import java.util.ArrayList;
import java.util.List;

public class GuideLayout extends BaseView {
    private SkyAppPager mPager = null;
    private List<Bitmap> mBitmaps = new ArrayList<Bitmap>();
    private OnGuideVisibleCallback mCallback = null;
    private PageNum mPageNum = null;

    public GuideLayout(Context context, Looper looper) {
        super(context);
        initPager();
        mHandler = new Handler(looper) {
            public void handleMessage(Message msg) {
                LogUtils.d("guide", "add page " + msg.arg1);
                mPager.getChildAt(msg.arg1).setBackgroundDrawable(new BitmapDrawable(mBitmaps.get(msg.arg1)));
            }

            ;
        };
        //addPageNum();
    }

    public void setGuideVisibleCallback(OnGuideVisibleCallback callback) {
        mCallback = callback;
    }

    public interface OnGuideVisibleCallback {
        public void onGuideVisible(boolean visible);
    }

    private void initPager() {
        mPager = new SkyAppPager(mContext, Direction.HORIZONTAL);
        addView(mPager, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mPager.setAnimationDuring(300, 300);
        mPager.setFocusable(true);
        mPager.setFocusableInTouchMode(true);
        mPager.requestFocus();
        mPager.setOnKeyListener(this);
    }

    private void addPageNum() {
        mPageNum = new PageNum(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        addView(mPageNum, params);
    }

    Handler mHandler = null;

    public void setImages(final int[] ids) {
        int length = ids.length;
        for (int i = 0; i < length; i++) {
            ImageView imageView = new ImageView(mContext);
            mPager.addView(imageView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            final int j = i;
            ThreadPool.addTask(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = SkinManager.getInstance().getBitmap(null, ids[j]);
                    mBitmaps.add(bitmap);
                    Message msg = mHandler.obtainMessage();
                    msg.arg1 = j;
                    if (j == 0) {
                        mHandler.sendMessage(msg);
                    } else {
                        mHandler.sendMessageDelayed(msg, 300);
                    }
                }
            });
        }
        // mPageNum.updatePageNum(length, 0);
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    if (mPager.getDisplayedChild() != 0) {
                        mPager.displayPrePage();
                        mPager.showPrevious();
                        //mPageNum.updatePageNum(mPager.getChildCount(), mPager.getDisplayedChild());
                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    LogUtils.d("guide", "display " + mPager.getDisplayedChild() + " and count " + mPager.getChildCount());
                    if (mPager.getDisplayedChild() != mPager.getChildCount() - 1) {
                        mPager.displayNextPage();
                        mPager.showNext();

                        //mPageNum.updatePageNum(mPager.getChildCount(), mPager.getDisplayedChild());
                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    if (mPager.getDisplayedChild() == mPager.getChildCount() - 1) {
                        if (mCallback != null) {
                            mCallback.onGuideVisible(false);
                        }
                        setVisibility(View.GONE);
                        LiteAppApplication.saveShowGuide();
                    }
                    break;
                default:
                    break;
            }
        }

        return super.onKey(v, keyCode, event);
    }


    public void destory() {
        for (Bitmap bitmap : mBitmaps) {
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
                bitmap = null;
            }
        }
        System.gc();
        mBitmaps.clear();
    }
}
